package com.app.car.greendao;


import com.alibaba.fastjson.annotation.JSONField;
import com.app.car.bean.BaseDataDeviceListBean;
import com.app.car.bean.BaseDataDeviceListData;
import com.app.car.bean.BaseDataDeviceListDataDao;
import com.app.car.bean.RfidListBean;
import com.app.car.bean.RfidListBeanDao;
import com.app.car.bean.RfidListData;
import com.app.car.bean.RfidListDataDao;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class GreenDaoUtils {

    private static BaseDataDeviceListDataDao deviceListDataDao = GreenDaoManager.getInstance().getmDaoSession().getBaseDataDeviceListDataDao();
    private static RfidListBeanDao rfidListBeanDao = GreenDaoManager.getInstance().getmDaoSession().getRfidListBeanDao();
    private static RfidListDataDao rfidDataDao = GreenDaoManager.getInstance().getmDaoSession().getRfidListDataDao();


    public static final String
            TYPE_GOODS= "goodsType" //货品类型
            ,TYPE_DEVICE = "device" //设备类型
            ,TYPE_RFID = "rfid" //箱码
            ;


    public static void clearAll(){
        deviceListDataDao.deleteAll();
        rfidListBeanDao.deleteAll();
        rfidDataDao.deleteAll();
    }


    /**
     * 增加
     **/
    public static void baseBeanInsertData(BaseDataDeviceListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        deviceListDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }


    public static void rfidBeanInsertData(RfidListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null) return;
        try{
            rfidListBeanDao.insertOrReplaceInTx(infoBean);
            callBack.onDataInsert(1);
        }catch (Exception e){
            callBack.onDataInsert(0);
            throw e;
        }
    }

    public static void rfidListDataInsertData(ArrayList<RfidListData> infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null) return;
        try{
            rfidDataDao.insertOrReplaceInTx(infoBean);
            callBack.onDataInsert(1);
        }catch (Exception e){
            callBack.onDataInsert(0);
            throw e;
        }
    }


    /**
     * 查找
     **/


    public static BaseDataDeviceListBean getBaseDeviceDate() {
        List<BaseDataDeviceListData> list = deviceListDataDao.queryBuilder().build().list();
        BaseDataDeviceListBean locationListBean = new BaseDataDeviceListBean();
        for (BaseDataDeviceListData data : list) {
            locationListBean.getList().add(data);
        }
        return locationListBean;
    }

    public static List<RfidListData> getRfidListData() {
        List<RfidListData> list = rfidDataDao.queryBuilder().build().list();
        return list;
    }

    public static List<RfidListData> getRfidListDataByWare(String ware) {
        List<RfidListData> list = rfidDataDao.queryBuilder().where(RfidListDataDao.Properties.Ware.eq(ware)).list();
        return list;
    }

    public static RfidListData getRfidListDataByRfid(String rfid) {
        List<RfidListData> list = rfidDataDao.queryBuilder().where(RfidListDataDao.Properties.Rfid.eq(rfid)).list();
        RfidListData data = new RfidListData();
        data.setRfid(rfid);
        if(list!= null || list.size()>0){
            for (RfidListData forData : list){
                    data.setWare(forData.getWare());
                    data.setType(forData.getType());
                    data.setGid(forData.getGid());
                    data.setName(forData.getName());
                    data.setSpecs(forData.getSpecs());
            }
            return data;
        }else {
            return data;
        }
    }



    public static List<RfidListBean> getRfidListBean() {
        List<RfidListBean> list = rfidListBeanDao.queryBuilder().orderDesc(RfidListBeanDao.Properties.Time).build().list();
        return list;
    }

    public static RfidListBean getRfidListBeanById(long id) {
        List<RfidListBean> list = rfidListBeanDao.queryBuilder().where(RfidListBeanDao.Properties.Id.eq(id)).list();
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }



    /**
     * 删除
     */
    public static void rfidBeanDeleteData(RfidListBean rfidListBean) {
        rfidListBeanDao.delete(rfidListBean);
    }

    public static void rfidListBeanDeleteAll() {
        rfidListBeanDao.deleteAll();
    }

    public static void rfidListDataDeleteAll(){
        rfidDataDao.deleteAll();
    }




    public static class EventDateChange {
        public static final String TYPE_ALL = "99";
        public String type;

        public EventDateChange(String type) {
            this.type = type;
        }
    }

    public interface GreenDaoInserCallBack {
        void onDataInsert(int count);
    }

    @JSONField(serialize = false)
    public static String getTypeName(String map_type) {
        switch (map_type) {
            case GreenDaoUtils.TYPE_DEVICE:
                return "设备";
            default:
                return "未知";
        }
    }
}
